require "argot"
require "DictionaryLoader"
require "BookArgot"
require "BookArrayArgot"
require "CommandBookstore"
require "Bookstore"
require "Book"
require "Utils"

LocalBookstore = { }
LocalBookstore_mt = { __index = LocalBookstore }

--[[
	 The Argot type system must be primed on start up of the application.
	 
	 Two data dictionary files are loaded.  The common.dictionary contains
	 various basic data types such as u8ascii.  The bookstore contains
	 our applications book and booklist type descriptions.
	 
	 After the dictionaries are loaded the marshalling functions are bound
	 to the data type.  The Book only binds the TypeWriter.  The reading is
	 handled automatically by the TypeReaderAuto marshaller.
  ]]

function LocalBookstore.create(self)
	local attributes = {}
    setmetatable( attributes, LocalBookstore_mt )
    return attributes
end


function LocalBookstore.setupArgot(self)
    local library = argot.TypeLibrarySingleton_getDefault()

    local dictionary_map = argot.TypeMap(library)
    argot.lua_read_dictionary( library, DictionaryLoader.getDictionaryStream("dictionary/common.dictionary"), dictionary_map )
    local dictionary_map2 = argot.TypeMap(library)
    argot.lua_read_dictionary( library, DictionaryLoader.getDictionaryStream("dictionary/bookstore.dictionary"), dictionary_map2 )
    
    argot.bind_common( library )

	local reader = argot.TypeReaderAuto( BookArgot.construct )
	library:Bind( BookArgot.TYPENAME, makeBoundFunction(reader, reader.read), BookArgot.write )
    library:Bind( BookArrayArgot.TYPENAME, BookArrayArgot.read, BookArrayArgot.write )
end

function LocalBookstore.main(self)
    self:setupArgot()

    local bookstore = Bookstore:create()
    local command = CommandBookstore:create( bookstore )
    
    command:getCommand()
end

localbookstore = LocalBookstore:create()
localbookstore:main()
